package com.lizemin.util;

import cn.hutool.core.io.resource.ResourceUtil;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.Map;

public class YamlParser {

    /**
     * 获取 Nacos 服务器地址
     *
     * @return nacos 服务器地址
     */
    public static String getNacosServerAddr() {
        Yaml yaml = new Yaml();
        // 读取 YAML 文件
        InputStream inputStream = ResourceUtil.getStream("application.yml");
        // 解析 YAML 文件为 Map 对象
        Map<String, Object> data = yaml.load(inputStream);

        // 获取 spring 节点
        Map<String, Object> spring = (Map<String, Object>) data.get("spring");
        if (spring != null) {
            // 获取 cloud 节点
            Map<String, Object> cloud = (Map<String, Object>) spring.get("cloud");
            if (cloud != null) {
                // 获取 nacos 节点
                Map<String, Object> nacos = (Map<String, Object>) cloud.get("nacos");
                if (nacos != null) {
                    // 获取 discovery 节点
                    Map<String, Object> discovery = (Map<String, Object>) nacos.get("discovery");
                    if (discovery != null) {
                        // 获取 server-addr 的值
                        return (String) discovery.get("server-addr");
                    }
                }
            }
        }
        return null;
    }


}